package com.supermarket.dao;

import com.supermarket.entity.SupplierEntity;
import java.sql.*;
import java.util.ArrayList;

public class SupplierDao {

    private static String DB_URL = "";
    private static String DB_USER = "";
    private static String DB_PASSWORD = "";

    // 数据库信息设置器
    public static void MySQLSettingForSupplier(String DB_URL1,String DB_USER1,String DB_PASSWORD1){
        DB_URL = DB_URL1;
        DB_USER = DB_USER1;
        DB_PASSWORD = DB_PASSWORD1;
    }

    // 获取供应商列表
    public ArrayList<SupplierEntity> allSuppliers() {
        String query = "SELECT * FROM supplier";
        return executeQuery(query);
    }

    // 通过ID查询供应商
    public SupplierEntity getSupplierById(Long supplierId) {
        if (supplierId == null) {
            return null;
        }
        String query = "SELECT * FROM supplier WHERE id = ?";
        ArrayList<SupplierEntity> results = executeQuery(query, supplierId);
        return results.isEmpty() ? null : results.get(0);
    }

    // 添加供应商
    public int insertSupplier(SupplierEntity supplier) {
        String query = "INSERT INTO supplier (name, address, phone) VALUES (?, ?, ?)";
        return executeUpdate(query, supplier.getName(), supplier.getAddress(), supplier.getPhone());
    }

    // 修改供应商信息
    public int updateSupplier(SupplierEntity supplier) {
        String query = "UPDATE supplier SET name = ?, address = ?, phone = ? WHERE id = ?";
        return executeUpdate(query, supplier.getName(), supplier.getAddress(), supplier.getPhone(), supplier.getId());
    }

    // 删除供应商
    public int deleteSupplier(SupplierEntity supplier) {
        String query = "DELETE FROM supplier WHERE id = ?";
        return executeUpdate(query, supplier.getId());
    }

    // 执行查询操作
    private ArrayList<SupplierEntity> executeQuery(String query, Object... params) {
        ArrayList<SupplierEntity> suppliers = new ArrayList<>();
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement statement = prepareStatement(connection, query, params);
             ResultSet resultSet = statement.executeQuery()) {

            while (resultSet.next()) {
                Long id = resultSet.getLong("id");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                String phone = resultSet.getString("phone");
                SupplierEntity supplier = new SupplierEntity(id, name, address, phone);
                suppliers.add(supplier);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return suppliers;
    }

    // 更新
    private int executeUpdate(String query, Object... params) {
        try (Connection connection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement statement = prepareStatement(connection, query, params)) {

            return statement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        }
    }

    private PreparedStatement prepareStatement(Connection connection, String query, Object... params) throws SQLException {
        PreparedStatement statement = connection.prepareStatement(query);
        for (int i = 0; i < params.length; i++) {
            statement.setObject(i + 1, params[i]);
        }
        return statement;
    }
}